<?php

class LoginView {
	// Define and assign private variables
	private $username = "username";
	private $password = "password";
	private $loginButton = "login";
	private $logoutButton = "logout";
	private $rememberMe = "rememberMe";
	public $errormessage;

	public function createContainer() {
		return "<div class='box'>";
	}

	public function createLoginBox() {
		// Returns xhtml string (a loginbox)
		$status = "<p class='status'>Du är utloggad</p>";
		$inputUsername = NULL;
		if (isset($_POST['username'])) {
			$inputUsername = $_POST['username'];
		}
		$loginbox = "<form action='' method='post' class='loginform'>
					<fieldset>
						<label for='$this->username'>Användarnamn:</label> <input type='text' id='$this->username' name='$this->username' value='$inputUsername'/> <br /> 
						<label for='$this->password'>Lösenord:</label> <input type='password'  id='$this->password' name='$this->password' /> <br />
						<input type='submit' name='$this->loginButton' value='Logga in' /> <label for='$this->rememberMe'>Kom ihåg mig</label> <input type='checkbox' checked='checked' name='$this->rememberMe' id='$this->rememberMe'/>
					</fieldset>
					</form>";
		$register = "<p>Har du ingen användare? <a href='?register=true'>Registrera dig här!</a></p>";
		return $status . $loginbox . $register;
	}

	public function showFeedback($action) {
		// Returns passed value enclosed by paragraph tags
		$msg = "<p class='error'>$action</p>";
		return $msg;
	}

	public function createLoggedInBox() {
		// Returns xhtml string (a logout button)
		$logoutbox = "<form action='' method='post' class='loginform'> 
				<input type='submit' name='$this->logoutButton' value='Logga ut' />
				</form>";
		$status = "<p class='status'>Du är inloggad. Välkommen!</p>";
		return $status . $logoutbox;
	}

	public function getUserName() {
		// Get and return username, if name has been input...
		if (isset($_POST[$this -> username])) {
			return $_POST[$this -> username];
		}
		// ...or if there's a cookie saved
		else if (isset($_COOKIE[$this -> username])) {
			return $_COOKIE[$this -> username];
		} else {
			Feedback::addError(Feedback::noUsername);
			return null;
		}
	}

	public function getPassword() {
		// Get and return username, if name has been input...
		if (isset($_POST[$this -> password])) {
			return $_POST[$this -> password];
		}
		// ...or if there's a cookie saved
		else if (isset($_COOKIE[$this -> password])) {
			return $_COOKIE[$this -> password];
		} else {
			Feedback::addError(Feedback::noPassword);
			return null;
		}
	}

	public function createCookie($name, $value) {
		// Set cookie
		setcookie($name, $value, time() + 3600);
		// For automatic testing
		$_COOKIE[$name] = $value;
	}

	public function destroyCookie($name) {
		// Destroy cookie
		setcookie($name, "", time() - 3600);
		unset($_COOKIE[$name]);
	}

	public function rememberMe() {
		// Returns true if user checked 'Remember me'- checkbox
		return isset($_POST[$this -> rememberMe]);
	}

	public function setOKLogin($username, $password) {
		if ($this -> rememberMe()) {
			$this -> createCookie($this -> username, $_POST[$this -> username]);
			$this -> createCookie($this -> password, $_POST[$this -> password]);
		}
	}

	public function unsetOKLogin() {
		// Destroy cookies
		$this -> destroyCookie($this -> username);
		$this -> destroyCookie($this -> password);
	}

	public function triedToLogin() {
		// Returns true if login button has been clicked
		if (isset($_POST[$this -> loginButton]) == true) {
			return true;
		}
		// or if there's a cookie saved
		else if (isset($_COOKIE[$this -> username]) && isset($_COOKIE[$this -> password])) {
			return true;
		}
		// else return false
		else {
			return false;
		}
	}

	public function triedToLogout() {
		// Returns true if logout button has been clicked, else returns false
		if (isset($_POST[$this -> logoutButton]) == true) {
			return true;
		} else {
			return false;
		}
	}

}
?>